package tiepbm.app.util.text;

import java.lang.reflect.Field;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import android.content.Context;
import android.text.TextUtils;
import android.util.Patterns;

public class ValidateUtil {
	
	private static Pattern mPattern;
	private static Matcher mMatcher;

	private static String mDomainName = "";
	private static final String USERNAME_REGEX = "^[a-z0-9_-]{3,15}$";

	// private static final String PHONE_REGEX = "^\\+[0-9]{10,13}$";

	/**
	 * Check user name is valid
	 * 
	 * @param userName
	 */
	public static boolean validateUserName(String userName) {
		mPattern = Pattern.compile(USERNAME_REGEX);
		mMatcher = mPattern.matcher(userName);
		return mMatcher.matches();
	}

	/**
	 * Check number phone is valid
	 */
	public static boolean validatePhoneNumber(String phoneNumber) {
		mPattern = Patterns.PHONE;
		return mPattern.matcher(phoneNumber).matches();

	}

	/**
	 * Check email is invalid
	 * 
	 * @param email
	 * @return
	 */
	public static boolean validateEmail(String email) {

		boolean matchFound = false;

		if (mDomainName != null && mDomainName.length() > 0) {

			// Set the email pattern string
			Pattern p = Pattern.compile(".+@" + mDomainName);
			// Match the given string with the pattern
			Matcher m = p.matcher(email);
			// check whether match is found
			matchFound = m.matches();

			if (matchFound)
				return true;
			else
				return false;
		} else {

			// Set the email pattern string
			Pattern p = Pattern.compile(".+@.+\\.[a-z]+");
			// Match the given string with the pattern
			Matcher m = p.matcher(email);
			// check whether match is found
			matchFound = m.matches();
		}

		if (matchFound)
			return true;
		else
			return false;
	}

	/**
	 * remove all characters isn't show ( umber acsii < 32)
	 * 
	 * @param text
	 * @return
	 */
	public static String removeAllSpecialCharacter(String text) {

		String result = "";
		if (!TextUtils.isEmpty(text)) {
			for (int i = 0; i < text.length(); i++) {
				if ((int) text.charAt(i) >= 32 && (int) text.charAt(i) <= 255) {
					result += text.charAt(i);
				}
			}
		}
		text = null;
		return result;
	}

	/**
	 * Get resource id from string
	 * 
	 * @param variableName
	 *            name of resource
	 * @param context
	 * @param c
	 * @return
	 */
	public static int getResIdFromString(String variableName, Context context,
			Class<?> c) {

		try {
			Field idField = c.getDeclaredField(variableName);
			return idField.getInt(idField);
		} catch (Exception e) {
			e.printStackTrace();
			return -1;
		}
	}
	
}
